<?php
namespace Home\Controller;

use Common\Controller\HomeController;

class ContainerController extends HomeController
{
    /**
     * 货柜列表
     * @author Mr.Alien
     * @return string
     */
    public function index($p = 1)
    {
        I('get.no') && ($this->where['no'] = array('like','%'.I('get.no').'%'));
        $this->where['user_id'] = session('id');
        parent::index($p);
    }

    /**
     * 保存货柜数据
     * @author Mr.Alien
     * @return string
     */
    public function save()
    {
        $_POST['user_id'] = session('id');
        $_POST['available'] = I('post.capacity') - I('post.used');
        parent::save();
    }

    /**
     * 禁用用户
     * @author Mr.Alien
     */
    public function forbidden()
    {
        $data = I('post.');
        $has = M("Container")->where(array('id'=>$data['id']))->find();
        if($data['status'] == 1 && ($has['used'] != '0')){
           $this->error('有库存无法禁用');
        }
        $res = M('Container')->where(array('id'=>$data['id']))->save(array('status'=>$data['status']));
        if($res === false){
            $this->error('保存失败');
        }else{
            $this->success('保存成功');
        }
    }
}